package com.daojia.daojiasync.entity;

import com.daojia.daojiasync.Utils.TimeUtils;
import com.daojia.daojiasync.dto.ApiResponse;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Objects;


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "work_orders")
public class WorkOrder {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "address")
    private String address;

    @Column(name = "addressDup")
    private String addressDup;

    @Column(name = "candidate")
    private String candidate;

    @Column(name = "candidateAttendant")
    private String candidateAttendant;

    @Column(name = "candidateBranch")
    private String candidateBranch;

    @Column(name = "candidatePm")
    private String candidatePm;

    @Column(name = "carBrand")
    private String carBrand;

    @Column(name = "companyId")
    private Integer companyId;

    @Column(name = "companyOrderNumber")
    private String companyOrderNumber;

    @Column(name = "createBy")
    private Integer createBy;

    @Column(name = "createTime")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Column(name = "creator")
    private Integer creator;

    @Column(name = "dotId")
    private Integer dotId;

    @Column(name = "isDelete")
    private Integer isDelete;

    @Column(name = "modifyTime")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime modifyTime;

    @Column(name = "pmId")
    private Integer pmId;

    @Column(name = "postcode")
    private String postcode;

    @Column(name = "templateId")
    private Integer templateId;

    @Column(name = "ticketStatus")
    private Integer ticketStatus;

    @Column(name = "tslPresurvey")
    private Boolean tslPresurvey;

    @Column(name = "userName")
    private String userName;

    @Column(name = "userPhone")
    private String userPhone;

    @Column(name = "userType")
    private Integer userType;

    @Column(name = "worderExecStatus")
    private Integer worderExecStatus;

    @Column(name = "worderExecStatusValue")
    private String worderExecStatusValue;

    @Column(name = "worderId")
    private Integer worderId;

    @Column(name = "worderLevel")
    private String worderLevel;

    @Column(name = "worderNo")
    private String worderNo;

    @Column(name = "worderStatus")
    private Integer worderStatus;

    @Column(name = "worderStatusValue")
    private String worderStatusValue;

    @Column(name = "worderTypeId")
    private Integer worderTypeId;

    @Column(name = "worderTypeName")
    private String worderTypeName;

    @Column(name = "worderTypeList_fullName")
    private String worderTypeListFullName;

    @Column(name = "worderTypeList_createTime")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime worderTypeListCreateTime;

    @Column(name = "dotInformationEntity_createTime")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime dotInformationEntityCreateTime;

    @Column(name = "dotInformationEntity_dotId")
    private Integer dotInformationEntityDotId;

    @Column(name = "dotInformationEntity_isDelete")
    private Integer dotInformationEntityIsDelete;

    @Column(name = "sysMark")
    private String sysMark;

    public Long getCreateTimeForNum() {
        return this.createTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    public static WorkOrder flattenOrder(ApiResponse.WorkOrderItem item) {
        WorkOrder order = new WorkOrder();

        // 设置基本字段
        order.setAddress(item.getAddress());
        order.setAddressDup(item.getAddressDup());
        order.setCandidate(item.getCandidate());
        order.setCandidateAttendant(item.getCandidateAttendant());
        order.setCandidateBranch(item.getCandidateBranch());
        order.setCandidatePm(item.getCandidatePm());
        order.setCarBrand(item.getCarBrand());
        order.setCompanyId(item.getCompanyId());
        order.setCompanyOrderNumber(item.getCompanyOrderNumber());
        order.setCreateBy(item.getCreateBy());
        order.setCreateTime(TimeUtils.parseDateTime(item.getCreateTime()));
        order.setCreator(item.getCreator());
        order.setDotId(item.getDotId());
        order.setIsDelete(item.getIsDelete());
        order.setModifyTime(TimeUtils.parseDateTime(item.getModifyTime()));
        order.setPmId(item.getPmId());
        order.setPostcode(item.getPostcode());
        order.setTemplateId(item.getTemplateId());
        order.setTicketStatus(item.getTicketStatus());
        order.setTslPresurvey(item.getTslPresurvey());
        order.setUserName(item.getUserName());
        order.setUserPhone(item.getUserPhone());
        order.setUserType(item.getUserType());
        order.setWorderExecStatus(item.getWorderExecStatus());
        order.setWorderExecStatusValue(item.getWorderExecStatusValue());
        order.setWorderId(item.getWorderId());
        order.setWorderLevel(item.getWorderLevel());
        order.setWorderNo(item.getWorderNo());
        order.setWorderStatus(item.getWorderStatus());
        order.setWorderStatusValue(item.getWorderStatusValue());
        order.setWorderTypeId(item.getWorderTypeId());
        order.setWorderTypeName(item.getWorderTypeName());

        // 扁平化嵌套字段
        if (item.getWorderTypeList() != null && !item.getWorderTypeList().isEmpty()) {
            ApiResponse.WorderType wt = item.getWorderTypeList().get(0);
            order.setWorderTypeListFullName(wt.getFullName());
            order.setWorderTypeListCreateTime(TimeUtils.parseDateTime(wt.getCreateTime()));
        }

        if (item.getDotInformationEntity() != null) {
            ApiResponse.DotInformationEntity dei = item.getDotInformationEntity();
            order.setDotInformationEntityCreateTime(TimeUtils.parseDateTime(dei.getCreateTime()));
            order.setDotInformationEntityDotId(dei.getDotId());
            order.setDotInformationEntityIsDelete(dei.getIsDelete());
        }

        return order;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        WorkOrder workOrder = (WorkOrder) o;
        return Objects.equals(address, workOrder.address) && Objects.equals(addressDup, workOrder.addressDup) && Objects.equals(candidate, workOrder.candidate) && Objects.equals(candidateAttendant, workOrder.candidateAttendant) && Objects.equals(candidateBranch, workOrder.candidateBranch) && Objects.equals(candidatePm, workOrder.candidatePm) && Objects.equals(carBrand, workOrder.carBrand) && Objects.equals(companyId, workOrder.companyId) && Objects.equals(companyOrderNumber, workOrder.companyOrderNumber) && Objects.equals(createBy, workOrder.createBy) && Objects.equals(createTime, workOrder.createTime) && Objects.equals(creator, workOrder.creator) && Objects.equals(dotId, workOrder.dotId) && Objects.equals(isDelete, workOrder.isDelete) && Objects.equals(modifyTime, workOrder.modifyTime) && Objects.equals(pmId, workOrder.pmId) && Objects.equals(postcode, workOrder.postcode) && Objects.equals(templateId, workOrder.templateId) && Objects.equals(ticketStatus, workOrder.ticketStatus) && Objects.equals(tslPresurvey, workOrder.tslPresurvey) && Objects.equals(userName, workOrder.userName) && Objects.equals(userPhone, workOrder.userPhone) && Objects.equals(userType, workOrder.userType) && Objects.equals(worderExecStatus, workOrder.worderExecStatus) && Objects.equals(worderExecStatusValue, workOrder.worderExecStatusValue) && Objects.equals(worderId, workOrder.worderId) && Objects.equals(worderLevel, workOrder.worderLevel) && Objects.equals(worderNo, workOrder.worderNo) && Objects.equals(worderStatus, workOrder.worderStatus) && Objects.equals(worderStatusValue, workOrder.worderStatusValue) && Objects.equals(worderTypeId, workOrder.worderTypeId) && Objects.equals(worderTypeName, workOrder.worderTypeName) && Objects.equals(worderTypeListFullName, workOrder.worderTypeListFullName) && Objects.equals(worderTypeListCreateTime, workOrder.worderTypeListCreateTime) && Objects.equals(dotInformationEntityCreateTime, workOrder.dotInformationEntityCreateTime) && Objects.equals(dotInformationEntityDotId, workOrder.dotInformationEntityDotId) && Objects.equals(dotInformationEntityIsDelete, workOrder.dotInformationEntityIsDelete);
    }

    @Override
    public int hashCode() {
        return Objects.hash(address, addressDup, candidate, candidateAttendant, candidateBranch, candidatePm, carBrand, companyId, companyOrderNumber, createBy, createTime, creator, dotId, isDelete, modifyTime, pmId, postcode, templateId, ticketStatus, tslPresurvey, userName, userPhone, userType, worderExecStatus, worderExecStatusValue, worderId, worderLevel, worderNo, worderStatus, worderStatusValue, worderTypeId, worderTypeName, worderTypeListFullName, worderTypeListCreateTime, dotInformationEntityCreateTime, dotInformationEntityDotId, dotInformationEntityIsDelete);
    }
}